package com.ejuornal.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.bean.Category;
import com.ejuornal.service.CategoryService;
import com.ejuornal.springutil.SpringUtil;
import com.ejuornal.util.StrToIntArray;
import com.google.gson.Gson;

/**
 * @Description:期刊分类action
 * @author lwf
 */
public class Category1Action extends BaseAction<Category> {
	private static final long serialVersionUID = 1L;
	private CategoryService categoryService;
	private Category category;
	private String categoryId;
	private String data;
	
	/**
	 * @Description:添加期刊分类
	 * @Author：lwf
	 * @return
	 */
	public String addCategory(){
		categoryService = (CategoryService) SpringUtil.getBean("categoryService");
		categoryService.add(category);
		return SUCCESS;
	}
	
	/**
	 * @Description:获取所有的期刊分类
	 * @Author:lwf
	 * @return
	 */
	public String getAllCategory(){
		categoryService = (CategoryService) SpringUtil.getBean("categoryService");
		List<Category> allCategoryList = new ArrayList<Category>();
		allCategoryList = categoryService.findAll();
		super.setRequestAttribute("allCategoryList", allCategoryList);
		return "allCategoryList";
	}
	
	/**
	 * @Description:获取所有的期刊分类，使用ajax请求
	 * @Author:syb
	 * @return
	 */
	public void getAllCategory2(){
		categoryService = (CategoryService) SpringUtil.getBean("categoryService");
		List<Category> allCategoryList = categoryService.findAll();
		
		Gson gson = new Gson();
		data = gson.toJson(allCategoryList);
		
		HttpServletResponse res = ServletActionContext.getResponse();   
		res.reset();   
		res.setContentType("text/html;charset=utf-8");   
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.print(data);
			pw.flush();   
			pw.close();  
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Decription:读取数据到页面实现修改
	 * @Author：lwf
	 * @return
	 */
	public String ShowCategoryInfo(){
		categoryService = (CategoryService) SpringUtil.getBean("categoryService");
		int categoryIdInt = Integer.parseInt(categoryId);
		category = categoryService.findById(categoryIdInt);
		return "categoryInfo";
	}
	
	/**
	 * @Decription:修改期刊分类
	 * @Author：lwf
	 * @return
	 */
	public String updateCategoryInfo(){
		categoryService = (CategoryService) SpringUtil.getBean("categoryService");
		int categoryIdStr = category.getCategoryId();
		categoryService.update(categoryIdStr, category);
		return "updateCategoryInfo";
	}
	
	public String deleteCategory(){
		categoryService.delete(StrToIntArray.ChangeStrToIntArray(categoryId));
		return "deleteCategory";
	}
	
	
	public CategoryService getCategoryService() {
		return categoryService;
	}
	
	@Resource(name="categoryService")
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
}
